import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

import store from "@/store"

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // },
  {
    path: '/goods',
    name: 'Goods',
    component: () => import(/* webpackChunkName: "goods" */ '../views/Goods.vue')
  },
  {
    path: '/order',
    name: 'Order',
    component: () => import(/* webpackChunkName: "order" */ '../views/Order.vue')
  },
  {
    path: '/user',
    redirect:"/user/cart",
  },
  {
    path: '/user',
    name: 'User',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
    // 子路由
    children: [
      {
        // 绝对路径
        // path: '/user/center',
        // 相对路径
        path:'center',
        component: () => import('../components/user/Center.vue')
      },
      {
        path:'cart',
        component: () => import('../components/user/Cart.vue')
      },
      {
        path:'detail',
        component: () => import('../components/user/Detail.vue')
      },
      {
        path:'message',
        component: () => import('../components/user/Message.vue')
      },
      {
        path:'order',
        component: () => import('../components/user/Order.vue')
      },
      {
        path:'attack',
        component: () => import('../components/user/Attack.vue')
      },
     
    ]
  },
  {
    path: '/free',
    name: 'Free',
    component: () => import(/* webpackChunkName: "free" */ '../views/Free.vue')
  },
  {
    path: '/free',
    redirect:"*",
  },
  {
    path: '/details',
    name: 'Details',
    component: () => import(/* webpackChunkName: "details" */ '../views/Details.vue')
  },
  {
    path: '*',
    name: 'Error',
    component: () => import(/* webpackChunkName: "error" */ '../views/Error.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 在现有版本的vue-router中，重复点击同一个路由会出现报错。
// 解决方法：直接修改原型方法push
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};

// 导航守卫
// router.beforeEach((to, from, next) => {
//   // console.log(to.path,from.path);

//   // to.path   要跳转到的路由
//   // from.path   从哪个路径跳转的路由

//   // 1 判断是否跳转到user这个路由
//   //   如果是跳转到user路由，判断是否已登录（token值）如果已登录，放行，
//   //   如果不是跳转到user路由，return
//   if (to.path === "/user") {
//     let token = localStorage.getItem("x-auth-token");
//     if (token) {
//       next();
//     } else {
//       store.dispatch('ShowToast/asyncchanToShowtoast', {
//         msg: "请先登录",
//         type: "warning",
//       })
//     }
//     return
//   }
//   next()  //  放行（可以跳转到其他路由）
// })
export default router
